package com.axend.lib_base.view.dialog

import android.app.Dialog
import android.content.Context
import android.view.View
import android.view.View.OnClickListener
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.axend.lib_base.R
import com.axend.lib_base.databinding.BaseDialogCustomCalibrationBinding
import com.axend.lib_base.databinding.BaseDialogCustomFirmwareUpdateBinding
import com.axend.lib_base.ext.dp2px
import com.axend.lib_base.ext.roundToNearestInteger
import com.axend.lib_base.ext.toFloatOrDefault
import com.axend.lib_base.utils.log.MyLogUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

/**
 * 固件更新
 */
class CustomFirmwareUpdateDialog (context: Context) :
Dialog(context, R.style.Common_CustomDialog) {

    private var dialogBinding: BaseDialogCustomFirmwareUpdateBinding

    companion object {
        const val MODEL_SELECT_FILE = 0
        const val MODEL_LOADING=1
        const val MODEL_FAILED = 2
        const val MODEL_SUCCESS = 3
        const val MODEL_DISMISS = 4
    }


    init {
        dialogBinding = DataBindingUtil.inflate(
            layoutInflater,
            R.layout.base_dialog_custom_firmware_update,
            null,
            false
        )
        setContentView(dialogBinding.root)
        setCanceledOnTouchOutside(false)
        val window = window
        window!!.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        //设置提示框位置
        val lp = window!!.attributes
        //lp.gravity = Gravity.TOP | Gravity.LEFT;
        //lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        //lp.x = DisplayUtils.dip2px(53);
        //lp.y = DisplayUtils.dip2px(186);
        lp.width = dp2px(300f)
        window.attributes = lp
//        timeJob = CoroutineScope(Dispatchers.Main).launch {
//            createNumberFlow(maxProgress).collect {
//                dialogBinding.ctProgressBar.setProgress(it.toFloatOrDefault(0f))
//                var percentage : Float= ((it / maxProgress) * 100).toFloatOrDefault(0f)
//                dialogBinding.ctProgressBar.setText("${percentage.roundToNearestInteger()}%")
//            }
//        }
    }


    private fun createNumberFlow(maxProgress: Int) = flow {
        for (i in 1..maxProgress) {
            emit(i)
            delay(50)
        }
    }

    /**
     * 设置模式 model 0 选择文件 1 加载  2 失败文案 3成功文案
     */
    fun setModel(model: Int): CustomFirmwareUpdateDialog {
        when (model) {
            MODEL_SELECT_FILE -> {
                dialogBinding.llSelectFile.visibility = View.VISIBLE
                dialogBinding.llProgress.visibility = View.GONE
                dialogBinding.llStatus.visibility = View.GONE
                dialogBinding.btCancel.setOnClickListener { dismiss() }
            }

            MODEL_LOADING -> {
                dialogBinding.llProgress.visibility = View.VISIBLE
                dialogBinding.llSelectFile.visibility = View.GONE
                dialogBinding.llStatus.visibility = View.GONE
            }

            MODEL_FAILED -> {
                dialogBinding.ctProgressBar.setText("")
                dialogBinding.llProgress.visibility = View.GONE
                dialogBinding.llSelectFile.visibility = View.GONE
                dialogBinding.llStatus.visibility = View.VISIBLE
                dialogBinding.ivStatus.setImageResource(R.drawable.icon_failed)
                dialogBinding.tvStatus.text = context.getString(R.string.app_tips_upgrade_failed)
                dialogBinding.btSure.text = context.getString(R.string.app_dialog_ok)
            }

            MODEL_SUCCESS -> {
                dialogBinding.ctProgressBar.setText("")
                dialogBinding.llProgress.visibility = View.GONE
                dialogBinding.llSelectFile.visibility = View.GONE
                dialogBinding.llStatus.visibility = View.VISIBLE
                dialogBinding.ivStatus.setImageResource(R.drawable.icon_ok)
                dialogBinding.tvStatus.text = context.getString(R.string.app_tips_upgrade_success)
                dialogBinding.btSure.text = context.getString(R.string.app_dialog_ok)
            }

            else -> {
                dismiss()
            }
        }
        return this
    }

    /**
     * 更新监听按钮
     */
    fun setUpgradeClickListener(listener: OnClickListener): CustomFirmwareUpdateDialog {
        dialogBinding.btUpgrade.setOnClickListener {
            listener.onClick(it)
        }
        return this
    }


    fun setDismissClickListener(listener: OnClickListener): CustomFirmwareUpdateDialog {
        dialogBinding.btSure.setOnClickListener {
            listener.onClick(it)
        }
        return this
    }



    /**
     * 文件路径设置
     */
    fun setFirmwarePath(path: String): CustomFirmwareUpdateDialog {
        dialogBinding.tvFirmwarePath.text = path
        return this
    }

    /**
     * 更新进度条
     */
    fun setProgress(progress: Int,maxProgress:Int): CustomFirmwareUpdateDialog {
        dialogBinding.ctProgressBar.setMaxProgress(maxProgress.toFloatOrDefault(0f))
        dialogBinding.ctProgressBar.setProgress(progress.toFloatOrDefault(0f))
        var percentage : Float= (progress.toFloat() / maxProgress) * 100
//        MyLogUtils.w("setProgress:${percentage}")
        dialogBinding.ctProgressBar.setText("${percentage.roundToNearestInteger()}%")
        return this
    }


    override fun dismiss() {
        super.dismiss()
    }


}